home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / Mesa-3.0 / SRC-GLU / TESS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-01  |  8.6 KB  |  363 lines

  1. /* $Id: tess.c,v 1.9 1998/06/01 01:10:29 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  2.6
  6.  * Copyright (C) 1995-1997  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: tess.c,v $
  26.  * Revision 1.9  1998/06/01 01:10:29  brianp
  27.  * small update for Next/OpenStep from Alexander Mai
  28.  *
  29.  * Revision 1.8  1998/02/04 00:27:58  brianp
  30.  * cygnus changes from Stephane Rehel
  31.  *
  32.  * Revision 1.7  1998/01/16 03:35:26  brianp
  33.  * fixed Windows compilation warnings (Theodore Jump)
  34.  *
  35.  * Revision 1.6  1997/09/17 01:51:48  brianp
  36.  * changed glu*Callback() functions to match prototype in glu.h
  37.  *
  38.  * Revision 1.5  1997/07/24 01:28:44  brianp
  39.  * changed precompiled header symbol from PCH to PC_HEADER
  40.  *
  41.  * Revision 1.4  1997/05/28 02:29:38  brianp
  42.  * added support for precompiled headers (PCH), inserted APIENTRY keyword
  43.  *
  44.  * Revision 1.3  1996/11/12 01:23:02  brianp
  45.  * added test to prevent free(vertex) when vertex==NULL in delete_contours()
  46.  *
  47.  * Revision 1.2  1996/10/22 22:57:19  brianp
  48.  * better error handling in gluBegin/EndPolygon() from Erich Eder
  49.  *
  50.  * Revision 1.1  1996/09/27 01:19:39  brianp
  51.  * Initial revision
  52.  *
  53.  */
  54.  
  55.  
  56. /*
  57.  * This file is part of the polygon tesselation code contributed by
  58.  * Bogdan Sikorski
  59.  */
  60.  
  61.  
  62. #ifdef PC_HEADER
  63. #include "all.h"
  64. #else
  65. #include <math.h>
  66. #include <stdlib.h>
  67. #include "tess.h"
  68. #endif
  69.  
  70.  
  71. /*
  72.  * This is ugly, but seems the easiest way to do things to make the
  73.  * code work under YellowBox for Windows
  74.  */
  75. #if defined(OPENSTEP) && defined(CALLBACK)
  76. #undef CALLBACK
  77. #define CALLBACK
  78. #endif
  79.  
  80.  
  81. extern void tess_test_polygon(GLUtriangulatorObj *);
  82. extern void tess_find_contour_hierarchies(GLUtriangulatorObj *);
  83. extern void tess_handle_holes(GLUtriangulatorObj *);
  84. extern void tess_tesselate(GLUtriangulatorObj *);
  85. extern void tess_tesselate_with_edge_flag(GLUtriangulatorObj *);
  86. static void delete_contours(GLUtriangulatorObj *);
  87.  
  88. #ifdef __CYGWIN32__
  89. #define _CALLBACK
  90. #else
  91. #define _CALLBACK CALLBACK
  92. #endif
  93.  
  94. void init_callbacks(tess_callbacks *callbacks)
  95. {
  96.    callbacks->begin = ( void (_CALLBACK*)(GLenum) ) 0;
  97.    callbacks->edgeFlag = ( void (_CALLBACK*)(GLboolean) ) 0;
  98.    callbacks->vertex = ( void (_CALLBACK*)(void*) ) 0;
  99.    callbacks->end = ( void (_CALLBACK*)(void) ) 0;
  100.    callbacks->error = ( void (_CALLBACK*)(GLenum) ) 0;
  101. }
  102.  
  103. void tess_call_user_error(GLUtriangulatorObj *tobj,
  104.     GLenum gluerr)
  105. {
  106.     if(tobj->error==GLU_NO_ERROR)
  107.         tobj->error=gluerr;
  108.     if(tobj->callbacks.error!=NULL)
  109.         (tobj->callbacks.error)(gluerr);
  110. }
  111.  
  112. GLUtriangulatorObj* APIENTRY gluNewTess( void )
  113. {
  114.    GLUtriangulatorObj *tobj;
  115.  
  116.     if((tobj=(GLUtriangulatorObj *)
  117.         malloc(sizeof(struct GLUtriangulatorObj)))==NULL)
  118.         return NULL;
  119.     tobj->contours=tobj->last_contour=NULL;
  120.     init_callbacks(&tobj->callbacks);
  121.     tobj->error=GLU_NO_ERROR;
  122.     tobj->current_polygon=NULL;
  123.     tobj->contour_cnt=0;
  124.     return tobj;
  125. }
  126.  
  127.  
  128. void APIENTRY gluTessCallback( GLUtriangulatorObj *tobj, GLenum which,
  129.                                void (CALLBACK *fn)() )
  130. {
  131.     switch(which)
  132.     {
  133.         case GLU_BEGIN:
  134.             tobj->callbacks.begin = (void (_CALLBACK*)(GLenum)) fn;
  135.             break;
  136.         case GLU_EDGE_FLAG:
  137.             tobj->callbacks.edgeFlag = (void (_CALLBACK*)(GLboolean)) fn;
  138.             break;
  139.         case GLU_VERTEX:
  140.             tobj->callbacks.vertex = (void (_CALLBACK*)(void *)) fn;
  141.             break;
  142.         case GLU_END:
  143.             tobj->callbacks.end = (void (_CALLBACK*)(void)) fn;
  144.             break;
  145.         case GLU_ERROR:
  146.             tobj->callbacks.error = (void (_CALLBACK*)(GLenum)) fn;
  147.             break;
  148.         default:
  149.             tobj->error=GLU_INVALID_ENUM;
  150.             break;
  151.     }
  152. }
  153.  
  154.  
  155.  
  156. void APIENTRY gluDeleteTess( GLUtriangulatorObj *tobj )
  157. {
  158.     if(tobj->error==GLU_NO_ERROR && tobj->contour_cnt)
  159.         /* was gluEndPolygon called? */
  160.         tess_call_user_error(tobj,GLU_TESS_ERROR1);
  161.     /* delete all internal structures */
  162.     delete_contours(tobj);
  163.     free(tobj);
  164. }
  165.  
  166.  
  167. void APIENTRY gluBeginPolygon( GLUtriangulatorObj *tobj )
  168. {
  169. /*
  170.     if(tobj->error!=GLU_NO_ERROR)
  171.         return;
  172. */
  173.         tobj->error = GLU_NO_ERROR;
  174.     if(tobj->current_polygon!=NULL)
  175.     {
  176.         /* gluEndPolygon was not called */
  177.         tess_call_user_error(tobj,GLU_TESS_ERROR1);
  178.         /* delete all internal structures */
  179.         delete_contours(tobj);
  180.     }
  181.     else
  182.     {
  183.         if((tobj->current_polygon=
  184.             (tess_polygon *)malloc(sizeof(tess_polygon)))==NULL)
  185.         {
  186.             tess_call_user_error(tobj,GLU_OUT_OF_MEMORY);
  187.             return;
  188.         }
  189.         tobj->current_polygon->vertex_cnt=0;
  190.         tobj->current_polygon->vertices=
  191.             tobj->current_polygon->last_vertex=NULL;
  192.     }
  193. }
  194.  
  195.  
  196. void APIENTRY gluEndPolygon( GLUtriangulatorObj *tobj )
  197. {
  198.     /*tess_contour *contour_ptr;*/
  199.  
  200.     /* there was an error */
  201.     if(tobj->error!=GLU_NO_ERROR) goto end;
  202.  
  203.     /* check if gluBeginPolygon was called */
  204.     if(tobj->current_polygon==NULL)
  205.     {
  206.         tess_call_user_error(tobj,GLU_TESS_ERROR2);
  207.         return;
  208.     }
  209.     tess_test_polygon(tobj);
  210.     /* there was an error */
  211.     if(tobj->error!=GLU_NO_ERROR) goto end;
  212.  
  213.     /* any real contours? */
  214.     if(tobj->contour_cnt==0)
  215.     {
  216.         /* delete all internal structures */
  217.         delete_contours(tobj);
  218.         return;
  219.     }
  220.     tess_find_contour_hierarchies(tobj);
  221.     /* there was an error */
  222.     if(tobj->error!=GLU_NO_ERROR) goto end;
  223.  
  224.     tess_handle_holes(tobj);
  225.     /* there was an error */
  226.     if(tobj->error!=GLU_NO_ERROR) goto end;
  227.  
  228.     /* if no callbacks, nothing to do */
  229.     if(tobj->callbacks.begin!=NULL && tobj->callbacks.vertex!=NULL &&
  230.         tobj->callbacks.end!=NULL)
  231.     {
  232.         if(tobj->callbacks.edgeFlag==NULL)
  233.             tess_tesselate(tobj);
  234.         else
  235.             tess_tesselate_with_edge_flag(tobj);
  236.     }
  237.  
  238. end:
  239.     /* delete all internal structures */
  240.     delete_contours(tobj);
  241. }
  242.  
  243.  
  244. void APIENTRY gluNextContour( GLUtriangulatorObj *tobj, GLenum type )
  245. {
  246.     if(tobj->error!=GLU_NO_ERROR)
  247.         return;
  248.     if(tobj->current_polygon==NULL)
  249.     {
  250.         tess_call_user_error(tobj,GLU_TESS_ERROR2);
  251.         return;
  252.     }
  253.     /* first contour? */
  254.     if(tobj->current_polygon->vertex_cnt)
  255.         tess_test_polygon(tobj);
  256. }
  257.  
  258.  
  259. void APIENTRY gluTessVertex( GLUtriangulatorObj *tobj, GLdouble v[3], void *data )
  260. {
  261.     tess_polygon *polygon=tobj->current_polygon;
  262.     tess_vertex *last_vertex_ptr;
  263.  
  264.     if(tobj->error!=GLU_NO_ERROR)
  265.         return;
  266.     if(polygon==NULL)
  267.     {
  268.         tess_call_user_error(tobj,GLU_TESS_ERROR2);
  269.         return;
  270.     }
  271.     last_vertex_ptr=polygon->last_vertex;
  272.     if(last_vertex_ptr==NULL)
  273.     {
  274.         if((last_vertex_ptr=(tess_vertex *)
  275.             malloc(sizeof(tess_vertex)))==NULL)
  276.         {
  277.             tess_call_user_error(tobj,GLU_OUT_OF_MEMORY);
  278.             return;
  279.         }
  280.         polygon->vertices=last_vertex_ptr;
  281.         polygon->last_vertex=last_vertex_ptr;
  282.         last_vertex_ptr->data=data;
  283.         last_vertex_ptr->location[0]=v[0];
  284.         last_vertex_ptr->location[1]=v[1];
  285.         last_vertex_ptr->location[2]=v[2];
  286.         last_vertex_ptr->next=NULL;
  287.         last_vertex_ptr->previous=NULL;
  288.         ++(polygon->vertex_cnt);
  289.     }
  290.     else
  291.     {
  292.         tess_vertex *vertex_ptr;
  293.  
  294.         /* same point twice? */
  295.         if(fabs(last_vertex_ptr->location[0]-v[0]) < EPSILON &&
  296.             fabs(last_vertex_ptr->location[1]-v[1]) < EPSILON &&
  297.             fabs(last_vertex_ptr->location[2]-v[2]) < EPSILON)
  298.         {
  299.             tess_call_user_error(tobj,GLU_TESS_ERROR6);
  300.             return;
  301.         }
  302.         if((vertex_ptr=(tess_vertex *)
  303.             malloc(sizeof(tess_vertex)))==NULL)
  304.         {
  305.             tess_call_user_error(tobj,GLU_OUT_OF_MEMORY);
  306.             return;
  307.         }
  308.         vertex_ptr->data=data;
  309.         vertex_ptr->location[0]=v[0];
  310.         vertex_ptr->location[1]=v[1];
  311.         vertex_ptr->location[2]=v[2];
  312.         vertex_ptr->next=NULL;
  313.         vertex_ptr->previous=last_vertex_ptr;
  314.         ++(polygon->vertex_cnt);
  315.         last_vertex_ptr->next=vertex_ptr;
  316.         polygon->last_vertex=vertex_ptr;
  317.     }
  318. }
  319.  
  320.  
  321. static void delete_contours(GLUtriangulatorObj *tobj)
  322. {
  323.     tess_polygon *polygon=tobj->current_polygon;
  324.     tess_contour *contour,*contour_tmp;
  325.     tess_vertex *vertex,*vertex_tmp;
  326.  
  327.     /* remove current_polygon list - if exists due to detected error */
  328.     if(polygon!=NULL)
  329.     {
  330.         if (polygon->vertices)
  331.         {
  332.             for(vertex=polygon->vertices;vertex!=polygon->last_vertex;)
  333.             {
  334.                 vertex_tmp=vertex->next;
  335.                 free(vertex);
  336.                 vertex=vertex_tmp;
  337.             }
  338.             free(vertex);
  339.         }
  340.         free(polygon);
  341.         tobj->current_polygon=NULL;
  342.     }
  343.     /* remove all contour data */
  344.     for(contour=tobj->contours;contour!=NULL;)
  345.     {
  346.         for(vertex=contour->vertices;vertex!=contour->last_vertex;)
  347.         {
  348.             vertex_tmp=vertex->next;
  349.             free(vertex);
  350.             vertex=vertex_tmp;
  351.         }
  352.         free(vertex);
  353.         contour_tmp=contour->next;
  354.         free(contour);
  355.         contour=contour_tmp;
  356.     }
  357.     tobj->contours=tobj->last_contour=NULL;
  358.     tobj->contour_cnt=0;
  359. }
  360.  
  361.  
  362.  
  363.